package com.zlt.dao.Impl;

import com.zlt.dao.VipDao;
import com.zlt.dto.VipLogDTO;
import com.zlt.entity.*;
import com.zlt.util.SqlUtil;

import java.util.List;

public class VipDaoImpl implements VipDao {
    @Override
    public List<Vip> selectVipAllType() {


        String sql=  "select vid, type, description,price,priority from vip";

        List<Vip> select = SqlUtil.select(Vip.class, sql);


        return select;
    }


    @Override
    public UserVip selectUserVip(String uid, int state) {

        String sql = "select * from user_vip where uid=? and state = ?";

        UserVip userVip = SqlUtil.selectOne(UserVip.class, sql, uid, 0);


        return userVip;
    }

    @Override
    public int updateVipExpired(String uid, int state) {

        String sql = "update user_vip set state = 1 where uid=? and state = ?";

        int i = SqlUtil.update(sql, uid, state);


        return i;
    }



    @Override
    public int insertChargeLog(VipLog vipLog) {

        int i = SqlUtil.insert(vipLog);


        return i;
    }

    @Override
    public Vip selectVipType(Integer vid) {


        String  sql  = "select * from vip where vid = ?";

        Vip vip = SqlUtil.selectOne(Vip.class, sql, vid);
        return vip;
    }

    @Override
    public int delayVip(UserVip userVip) {

        int i = SqlUtil.update(userVip);
        return i;
    }

    @Override
    public int createUserVip(UserVip userVip) {


        int insert = SqlUtil.insert(userVip);

        return insert;
    }




    @Override
    public List<UserVip> selectAllUserVip(int state) {

        String sql ="select uvid,uid,vid,start_time,end_time,state from user_vip where state = ?";

        List<UserVip> select = SqlUtil.select(UserVip.class, sql, state);


        return select;
    }




    @Override
    public Company selectVipCompany(String uid) {
        //对于这种很长的sql语句  参数要放在后面（？）
        String sql = "select company.cid,company.cname,company.address,company.legal_person,company.contact,company.phone,company.state,company.lid from company \n" +
                "left join user_company \n" +
                "on company.cid = user_company.cid\n" +
                "where company.state = 0\n" +
                "and  user_company.uid =?";

        Company company = SqlUtil.selectOne(Company.class, sql, uid);


        return company;
    }
    //
    @Override
    public List<Job> selectVipJobs(Integer cid, int state) {

        String sql = "select job.jid,job.jname,job.jtid,job.min_salary,job.max_salary,job.description,job.requirement,job.cid,job.work_address,job.uid,job.state,job.count \n" +
                "from job \n" +
                "where   state = ? \n" +
                "and  count>0 \n" +
                "and  cid=?";


        List<Job> select = SqlUtil.select(Job.class, sql, state, cid);


        return select;
    }

    @Override
    public List<Company> selectAllCompanys(int state) {


        String sql = "select company.cid,company.cname,company.address,company.legal_person,company.contact,company.phone,company.state,company.lid \n" +
                "from company \n" +
                "where state = ?";


        List<Company>  allCompanys  = SqlUtil.select(Company.class,sql,state);

        return allCompanys;
    }

    @Override
    public List<Job> selectRecentJobs(int limit) {

        String sql = "select job.jid,job.jname,job.jtid,job.min_salary,job.max_salary,job.description,job.requirement,job.cid,job.work_address,job.uid,job.state,job.count from job order by jid DESC LIMIT ?";

        List<Job> select = SqlUtil.select(Job.class, sql, limit);



        return select;
    }

    @Override
    public JobType selectJobType(Integer jtid) {

        String sql = "select  job_type.jtid,job_type.name,job_type.parent_id from job_type  where jtid= ?";

        JobType jobType = SqlUtil.selectOne(JobType.class, sql, jtid);

        return jobType;
    }

    @Override
    public Company selectCompany(Integer cid) {


        String sql = "select company.cid,company.cname,company.address,company.legal_person,company.contact,company.phone,company.state,company.lid \n" +
                "from company \n" +
                "where cid = ?";
        Company company = SqlUtil.selectOne(Company.class, sql, cid);
        return company;
    }



    @Override
    public List<VipLog> selectVipLogs(String uid) {


        String sql = "select vip_log.lid,vip_log.order_no,vip_log.uid,vip_log.create_time,vip_log.money,vip_log.vid  \n" +
                "from vip_log \n" +
                "where uid =?";

        List<VipLog> select = SqlUtil.select(VipLog.class, sql, uid);
        return select;
    }

    @Override
    public int insertChargeRecord(VipLogDTO vipLogDTO) {

        String sql = "insert into charge_record (uid,money,order_no,vid) VALUES(?,?,?,?)";

        int i = SqlUtil.insertReturnId(sql, vipLogDTO.getUid(), vipLogDTO.getMoney(), vipLogDTO.getOrderNo(),vipLogDTO.getVid());
        return i;
    }


}
